package yxy.game.pm2.module.bandit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.joda.time.DateTime;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.crusadedateduties;
import yxy.cherry.data.bean.herodata;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.task.TaskHandle;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

public class BanditHandle {
	public Result sendTask(String playerCode, int taskId, Collection<String> sending) {
		BanditModule banditModule = BanditModule.get(playerCode);

		try {
			if (banditModule.tryLock()) {
				Bandit bandit = banditModule.task(taskId);
				if (bandit == null || bandit.getEndTime() != null) {
					return Result.BanditTaskDeliverFail;// 任务派遣失败
				}

				ArrayList<Hero> sendingHero = new ArrayList<>();
				for (String code : sending) {
					Hero hero = Hero.get(playerCode, code);
					if (hero == null) {
						return Result.BanditTaskDeliverFail;// 任务派遣失败
					}
					if (banditModule.sendingHeroList().contains(hero.getCode())) {
						// 武将已经派遣了
						return Result.BanditHeroNotFit;// 派遣武将条件不符
					}
					sendingHero.add(hero);
				}

				crusadedateduties data = DataCenter.getData(bandit.getDataId(), crusadedateduties.class);
				{
					// 判断武将条件
					for (int i = 0; i < data.getType().length; i++) {
						int type = data.getType()[i];
						int param = data.getParameter()[i];
						if (!checkHeroParam(type, param, sendingHero)) {
							return Result.BanditHeroNotFit;// 派遣武将条件不符
						}
					}
				}
				{
					// 消费
					Currency cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
					try {
						new CurrencyHandle().cost(playerCode, CCReason.剿匪, cost);
					} catch (CurrencyNotEnoughException e) {
						return new Result(Result.CostFail, e.getType().getNumber(), e.getDataId());// 消费不足
					}
				}

				// 任务派遣
				bandit.setEndTime(DateTime.now().plusSeconds(data.getTime()));
				bandit.setSendingHeroCode(new ArrayList<>(sending));

				banditModule.set();

				TaskHandle.handle(playerCode, TaskType.统计型_剿匪_任务_品质_次数, 1, data.getQuality());
				TaskHandle.handle(playerCode, TaskType.统计型_剿匪_任务_品质_以上, data.getQuality());

				return Result.Success;

			}
		} catch (Exception e) {
		} finally {
			banditModule.unlock();
		}
		return Result.DataError;
	}

	private boolean checkHeroParam(int type, int param, Collection<Hero> heros) {
		if (type <= 0) {
			return true;
		}
		switch (type) {
		case 1:// 星级
		{
			for (Hero hero : heros) {
				if (hero.getStar() >= param) {
					return true;
				}
			}
		}
			break;
		case 2:// 阵营
		{
			for (Hero hero : heros) {
				herodata data = DataCenter.getData(hero.getDataId(), herodata.class);
				if (data.getPower() == param) {
					return true;
				}
			}
		}
			break;
		}
		return false;
	}

	public void updateBanditTip(String playerCode) {
		BanditModule banditModule = BanditModule.get(playerCode);
		for (Bandit bandit : banditModule.getTaskList()) {
			if (bandit.getEndTime() == null) {
				continue;
			}
			if (bandit.getEndTime().isAfter(DateTime.now())) {
				continue;
			}
			new TipHandle().updateTip(playerCode, TipType.征战_剿匪按钮, true);
			return;
		}
		new TipHandle().updateTip(playerCode, TipType.征战_剿匪按钮, false);
	}
}
